package com.alibaba.luohua.common.core.web.domain;

import com.alibaba.luohua.common.core.enums.ResultCode;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

import java.util.List;

/**
 * 分页数据结果集
 *
 * @author luohua
 * @date 2022年3月14日22:26:36
 */
@Data
@NoArgsConstructor
@EqualsAndHashCode(callSuper = true)
public class PageResult<T> extends BaseResult {

	private static final long serialVersionUID = -8109739154376004134L;

	/**
	 * 分页数据结果集
	 */
	private PageInfo<T> data = null;

	public PageResult(int code, String message) {
		super(code, message);
	}

	public PageResult(int code, String message, PageInfo<T> data) {
		super(code, message);
		this.data = data;
	}
	
	public PageResult<T> code(Integer code){
        this.setCode(code);
        return this;
    }
    
    public PageResult<T> message(String message){
        this.setMessage(message);
        return this;
    }
    
    public PageResult<T> data(PageInfo<T> data){
        this.setData(data);
        return this;
    }

	public static <T> PageResult<T> of(ResultCode status) {
		return new PageResult<>(status.code(), status.message());
	}

	public static <T> PageResult<T> success(PageInfo<T> data) {
		return new PageResult<>(ResultCode.SUCCESS.code(), 
				ResultCode.SUCCESS.message(), 
				data);
	}

	public static <T> PageResult<T> success(List<T> results) {
		return new PageResult<>(ResultCode.SUCCESS.code(), 
				ResultCode.SUCCESS.message(),
				PageInfo.of(results));
	}

	public static <T> PageResult<T> success(List<T> results, Long totalCount, Integer pageNum, Integer pageSize) {
		return new PageResult<>(ResultCode.SUCCESS.code(), 
				ResultCode.SUCCESS.message(),
				PageInfo.of(results, totalCount, pageNum, pageSize));
	}

}
